/**
 * @(#)CodeEntityHeadDependancyRelation.java, 2008-5-2.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie.analyzer.internal.relation;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Type;

import edu.pku.sei.pgie.analyzer.internal.CodeEntitySymbolTable;
import edu.pku.sei.pgie.analyzer.internal.VisitorWithContext;

/**
 *
 *
 * @author HeLi
 *
 */
public class CodeEntityHeadDependancyRelation extends
		AbstractCodeEntityRelation {

	public class HeadDependancyVisitor extends VisitorWithContext {
		List<String[]> container;

		public HeadDependancyVisitor(boolean enableAnonymousType) {
			super(enableAnonymousType);
			container = new ArrayList<String[]>();
		}

		@Override
		public boolean visit(MethodDeclaration node) {
			super.visit(node);
			Type returnType = node.getReturnType2();
			if(returnType != null ) {
				ITypeBinding returnTypeBinding = returnType.resolveBinding();
				if (returnTypeBinding != null) {
					String returnTypeQN = returnTypeBinding.getQualifiedName();
					container.add(new String[] { peekTypeFQN(), returnTypeQN });
				}
			}
			List parameters = node.parameters();
			for (int i = 0; i < parameters.size(); i++) {
				SingleVariableDeclaration sv = (SingleVariableDeclaration) parameters
						.get(i);
				Type type = sv.getType();
				ITypeBinding typeBinding = type.resolveBinding();
				if(typeBinding != null) {
					container.add(new String[] { peekTypeFQN(),
							typeBinding.getQualifiedName() });
				}
			}
			return false;
		}

		public List<String[]> getResult() {
			return container;
		}

	}

	public String[][] collectCallBack(CompilationUnit root,
			CodeEntitySymbolTable symbolTable) throws Exception {
		HeadDependancyVisitor visitor = new HeadDependancyVisitor(true);
		root.accept(visitor);
		List<String[]> result = visitor.getResult();
		return result.toArray(new String[result.size()][]);
	}

}
